import "babel-polyfill"

const path = require('path')
const cookieParser = require('cookie-parser')

const express = require('express')
const app = express()

//历史路由
// const history = require('connect-history-api-fallback')
// app.use('/',history())

// 别名
import { addAliases } from 'module-alias'
addAliases({
  "@": __dirname,
  "@utils": __dirname + "/utils",
  "@controllers": __dirname + "/controllers",
  "@maps": __dirname + "/maps"
})
// "_moduleAliases": {
//   "@": ".",
//   "@utils": "/utils",
//   "@contro": "/controllers",
//   "@maps": "/maps"
// },

// //设置跨域请求
// const { createProxyMiddleware } = require('http-proxy-middleware')
// app.use(createProxyMiddleware("/api",{
//   target:'http://localhost:3000/node/',
//   changeOrigoin:true,
//   pathRewrite: {'^/api':''},
// }))
// app.use(createProxyMiddleware("/selectip",{
//   target:'http://pv.sohu.com/cityjson?ie=utf-8',
//   changeOrigoin:true,
// }))
// app.use(createProxyMiddleware("/oper",{
//   target:'http://localhost:3000/node/spad/oper',
//   changeOrigoin:true,
// }))
// app.all('*', function (req, res, next) {
// 	res.header("Access-Control-Allow-Origin", "*")
// 	res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild')
// 	res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS")
// 	res.header("X-Powered-By", ' 3.2.1')
// 	res.header("Content-Type", "application/json;charset=utf-8")
// 	next()
// })

//解析JSON
//文件大小限制
const bodyParser = require('body-parser');
// 解析 application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))
// 解析 application/json
app.use(bodyParser.json({ limit: '50mb' }))

//请求头
const cors = require('cors')
app.use(cors());

//解析数据
import { dataAnalysis } from './utils/middleware.js'
app.use(dataAnalysis)

//托管静态路径
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))

//路由
app.use('/',  require('./routes/index'))
app.use('/node/user',  require('./routes/users'))
app.use('/node/else',  require('./routes/else'))
app.use('/node/upload',  require('./routes/upload'))
app.use('/node/cptt',  require('./routes/cptt'))
app.use('/node/ht',require('./routes/ht/htindex'))
app.use('/node/htcp',require('./routes/ht/htcp.js'))
app.use('/node/spad/oper',require('./routes/spad/oper.js'))

//启动服务
import { CORN_MAIN,CORN_ING } from './corn/index'
import { reCode } from './utils/application'
const http = require('http')
const server = http.createServer(app)
const SEND_ADMIN_CODE = false
server.listen(3000,()=>{
  console.log("运行成功: 127.0.0.1:3000")
  CORN_MAIN()//立即触发一次
  CORN_ING('0 0 4 * * *')//每天04:00:00触发
  reCode(SEND_ADMIN_CODE,'821783496@qq.com')
})